////基于C语言的ip地址16进制整型与字符串相转换的简单实现方式
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//
//unsigned int ipv4_str_to_hex(char *str) {
//	char ip1[4] = "";
//	char ip2[4] = "";
//	char ip3[4] = "";
//	char ip4[4] = "";
//	char *p = str;
//	strcpy(ip1, strsep(&p, "."));
//	strcpy(ip2, strsep(&p, "."));
//	strcpy(ip3, strsep(&p, "."));
//	strcpy(ip4, strsep(&p, "."));
//	unsigned int ip = atoi(ip1);
//	ip = ip * 256 + atoi(ip2);
//	ip = ip * 256 + atoi(ip3);
//	ip = ip * 256 + atoi(ip4);
//	return ip;
//}
//
//void ipv4_hex_to_str(unsigned int iphex, char *ipstr) {
//	sprintf(ipstr, "%u.%u.%u.%u", (iphex >> 24) & 0xff, (iphex >> 16) & 0xff, (iphex >> 8) & 0xff);
//}
//
//int main() {
//	char ipstr[64] = "3131372E37342E3133362E3334";
//	unsigned int a;
//	a = ipv4_str_to_hex(ipstr);
//	printf("hex ip:0x%08x\n", a);
//	char ipstr2[64] = "";
//	ipv4_hex_to_str(a, ipstr2);
//	printf("str ip is: %s\n", ipstr2);
//	return 0;
//}

#include <stdlib.h>
#include <string.h>

int hextodec(char *hex) {
	int sum = 0, t, i;

	for (i = 0; i < 2; i++, hex++) {
		if (*hex >= 'A' && *hex <= 'F')
			t = *hex - 'A' + 10;
		else
			t = *hex - '0';

		if (i == 0)
			sum += t * 16;
		else
			sum += t;
	}

	return sum;
}

void convert(char *dst, char *src) {
	int i = 0, len, t;
	char ch[2];
	len = strlen(src);

	if (len % 2 != 0) {
		printf("源字符串格式错误\n");
		return;
	}

	while (1) {
		ch[0] = *src++;
		ch[1] = *src++;
		t = hextodec(ch);

		if (t == 0x3b || i > len - 2) {
			*dst = '\0';
			return;
		}

		*dst = t;
		dst++;
		i += 2;
	}
}

int main() {
	char name[5][80] = {"63766E6176693B", "73686968616E6740323031393B", "3131372E37342E3133362E33343B", "32313B", "3037323946344236344D4D48455430365639"};
	char buf[100];

	for (int i = 0; i < 5 ; i++) {
		convert(buf, name[i]);
		printf("%s\n", buf);
	}

	return 0;
}
